﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace AusProject
{
    public partial class FormSupplierOrderFormChooser : Form
    {
        public FormSupplierOrderFormChooser()
        {
            InitializeComponent();
        }

        private bool accepted = false;

        public bool Accepted
        {
            get { return accepted; }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            accepted = true;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            accepted = false;
            this.Close();
        }


        public SupplierOrderFormInfo SelectedForm
        {
            get
            {
                return (SupplierOrderFormInfo)lstSupplierOrderForms.SelectedItem;
            }
        }

        private int componentID;

        public void SetComponentType(int componentID)
        {
            this.componentID = componentID;
            LoadSupplierList(componentID);
        }

        private void LoadSupplierList(int componentID)
        {
            lstSuppliers.Items.Clear();

            SupplierInfo si;

            if (componentID < 2)
            {
                si = new SupplierInfo(1, "Laminex");
                lstSuppliers.Items.Add(si);
            }

            if (componentID < 3)
            {
                si = new SupplierInfo(2, "Stone");
                lstSuppliers.Items.Add(si);
            }

            if (componentID < 4)
            {
                si = new SupplierInfo(3, "DX");
                lstSuppliers.Items.Add(si);
            }

            if (lstSuppliers.Items.Count > 0)
            {
                lstSuppliers.SelectedIndex = 0;
            }
            else
            {
                lstSupplierOrderForms.Items.Clear();
                lstSupplierOrderForms_SelectedIndexChanged(null, null);
                btnOK.Enabled = false;
            }
        }

        private void lstSuppliers_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstSuppliers.SelectedIndex >= 0)
            {
                LoadSupplierOrderFormList(componentID, ((SupplierInfo)lstSuppliers.SelectedItem).Id);
            }
            else
            {
                lstSupplierOrderForms.Items.Clear();
            }
            btnOK.Enabled = false;
        }

        private void LoadSupplierOrderFormList(int componentID, int supplierID)
        {
            lstSupplierOrderForms.Items.Clear();

            List<OrderDataControls.DataBoxInfo> listOfDataBox = new List<OrderDataControls.DataBoxInfo>();
            listOfDataBox.Add(new OrderDataControls.DataBoxInfo(1, OrderDataControls.DataBoxType.TextDataBox, "Supplier", true, OrderDataControls.DataControlBorderMode.Full, "Laminex", 20, 20, 200, 30, null, 13, "", new OrderDataControls.DataBoxFont("Microsoft Sans Serif", 8.25f, false, false)));
            listOfDataBox.Add(new OrderDataControls.DataBoxInfo(2, OrderDataControls.DataBoxType.NumberDataBox, "Quantity", true, OrderDataControls.DataControlBorderMode.Full, "5", 20, 60, 200, 30, null, 14, "", new OrderDataControls.DataBoxFont("Microsoft Sans Serif", 8.25f, false, false)));
            listOfDataBox.Add(new OrderDataControls.DataBoxInfo(3, OrderDataControls.DataBoxType.TextDataBox, "ID#", true, OrderDataControls.DataControlBorderMode.Full, "00789", 20, 100, 200, 30, null, 15, "", new OrderDataControls.DataBoxFont("Microsoft Sans Serif", 8.25f, false, false)));
            listOfDataBox.Add(new OrderDataControls.DataBoxInfo(4, OrderDataControls.DataBoxType.TextDataBox, "Type", true, OrderDataControls.DataControlBorderMode.Full, "Rank#A", 20, 140, 200, 30, null, 16, "", new OrderDataControls.DataBoxFont("Microsoft Sans Serif", 8.25f, false, false)));

            SupplierOrderFormInfo formInfo = new SupplierOrderFormInfo("Dat hang 1", 500, 250, "", listOfDataBox);

            lstSupplierOrderForms.Items.Add(formInfo);
        }

        private void lstSupplierOrderForms_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstSupplierOrderForms.SelectedIndex >= 0)
            {
                btnOK.Enabled = true;
            }
            else
            {
                btnOK.Enabled = false;
            }
        }

        private void lstSupplierOrderForms_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            int idx = lstSupplierOrderForms.IndexFromPoint(e.Location);
            if (idx >= 0)
            {
                lstSupplierOrderForms.SelectedIndex = idx;
                accepted = true;
                this.Close();
            }
        }
    }
}
